/*
 * Copyright (C) 2007-2008 SlideMe client
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.slideme.android.manager;

import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import org.slideme.android.manager.respoitory.UserInfoRepository;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

public class MyApplicationsActivity extends Activity {

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.myapp_layout);

        SamApplication samApplication = (SamApplication) getApplication();
        ApplicationManagerContext ctx = samApplication.getApplicationManagerContext();
        UserInfoRepository userInfoRepository = ctx.getUserInfoRepository();
        List<String> installedBundleIds = userInfoRepository.getUserInfo().getInstalledBundleIds();
        Cursor installedBundleIdsCursor = ctx.getContentBundleRepository().fetchCursorByInstalledBundleIds(installedBundleIds);
        if (installedBundleIdsCursor != null) {
            final ListView lv = (ListView) findViewById(R.id.myapp_layout_listview);

            String[] from = new String[]{"displayName", "vendorName", "iconFileUri", "category",
                    "version", "creationDate"};
            int[] to = new int[]{R.id.myapp_list_displayName,
                    R.id.myapp_list_vendor_name,
                    R.id.myapp_list_image,
                    R.id.myapp_list_category,
                    R.id.myapp_list_version,
                    R.id.myapp_list_date
            };
            SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.myapp_list_layout,
                    installedBundleIdsCursor, from, to);

            //Binder for reading icons off of file-system
            adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
                public boolean setViewValue(View view, Cursor cursor, int i) {
                    if (i < 0 || !(view instanceof ImageView)) return false;
                    if (i != cursor.getColumnIndex("iconFileUri")) {
                        return false;
                    }
                    ImageView iconImageView = (ImageView) view;
                    String fileUri = "/data/data/org.slideme.android.manager/files/"
                            + cursor.getString(i);

                    BufferedInputStream bis;
                    try {
                        bis = new BufferedInputStream(new FileInputStream(fileUri), 8192);
                    } catch (FileNotFoundException e) {
                        Log.i("MyApplicationsActivity", "Could not find icon: Name =" + fileUri, e);
                        return false;
                    }
                    Bitmap bm = BitmapFactory.decodeStream(bis);
                    try {
                        bis.close();
                    } catch (IOException e) {
                        Log.d("MyApplicationsActivity", "Failed to read icon: Name = " + fileUri, e);
                        return false;
                    }
                    iconImageView.setImageBitmap(bm);
                    return true;
                }
            });
            lv.setAdapter(adapter);

        }

        ImageButton myAppsView = (ImageButton) this.findViewById(R.id.myapp_layout_myapps_button);
        ImageButton settingsView = (ImageButton) this.findViewById(R.id.myapp_layout_settings_button);
        ImageButton applicationsView = (ImageButton) this.findViewById(R.id.myapp_layout_applications_button);
        ViewContext.setMenuViewListeners(myAppsView, settingsView, applicationsView, this);

    }
}



